﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Sliver.Scene.GameControl
{
    /// <summary>
    /// 处理各种非UI层次的输入事件
    /// </summary>
    public class GameInput
    {
        public static Vector3 MousePosition => UnityEngine.Input.mousePosition;

        public static bool AnyMouseInput => GameInput.TouchCount > 0 || GetMouseButtonDown(0) && GetMouseButtonDown(1);

        public static int TouchCount => UnityEngine.Input.touchCount;
        public static Touch[] Touches => UnityEngine.Input.touches;

        public enum BlockLevel
        {
        }

        // 默认状态为0
        public static int EnableFlag = 0x7FFFFFFF;

        public const int FlagScreenRaycastEnable = 1 << 1;
        public const int FlagClickEnable = 1 << 3;
        public const int FlagKeyboardEnable = 1 << 4;

        public static bool ScreenRaycastEnable
        {
            set
            {
                if (value)
                {
                    EnableFlag |= FlagScreenRaycastEnable;
                }
                else
                {
                    EnableFlag &= ~FlagScreenRaycastEnable;
                }
            }
            get => (EnableFlag & FlagScreenRaycastEnable) > 0;
        }

        public static bool ClickEnable
        {
            set
            {
                if (value)
                {
                    EnableFlag |= FlagClickEnable;
                }
                else
                {
                    EnableFlag &= ~FlagClickEnable;
                }
            }
            get => (EnableFlag & FlagClickEnable) > 0;
        }

        public static bool KeyboardEnable
        {
            set
            {
                if (value)
                {
                    EnableFlag |= FlagKeyboardEnable;
                }
                else
                {
                    EnableFlag &= ~FlagKeyboardEnable;
                }
            }
            get => (EnableFlag & FlagKeyboardEnable) > 0;
        }

        public static bool Paused { get; protected set; } = false;

        protected static int EnableFlagCache = EnableFlag;

        public static void EnableAll()
        {
            EnableFlag = EnableFlagCache;
        }

        public static void DisableAll()
        {
            EnableFlagCache = EnableFlag;
            EnableFlag = 0;
        }


        public static Camera RaycastCamera = null;

        public static Ray ScreenRay()
        {
            return Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
        }

        // public static bool ScreenRaycast(out RaycastHit hitInfo, int layerMask = Physics.DefaultRaycastLayers)
        // {
        //     if (Paused || !ScreenRaycastEnable)
        //     {
        //         hitInfo = new RaycastHit();
        //         return false;
        //     }
        //
        //     Vector2 point = UnityEngine.Input.mousePosition;
        //     if (!IsLegitimatePoint(point))
        //     {
        //         hitInfo = new RaycastHit();
        //         return false;
        //     }
        //
        //     return Physics.Raycast(Camera.main.ScreenPointToRay(point), out hitInfo, Mathf.Infinity, layerMask);
        // }

        /// <summary>
        /// 在没有UI的地方进行射线检测
        /// </summary>
        /// <param name="hitInfo"></param>
        /// <param name="layerMask"></param>
        /// <returns></returns>
        public static bool ScreenRaycastWithoutUI(out RaycastHit hitInfo, int layerMask = Physics.DefaultRaycastLayers)
        {
            Vector2 point = UnityEngine.Input.mousePosition;
            if (IsScreenPositionOverUI(point))
            {
                hitInfo = new RaycastHit();
                return false;
            }

            return Physics.Raycast(Camera.main.ScreenPointToRay(point), out hitInfo, Mathf.Infinity, layerMask);
        }

        protected static List<RaycastResult> RaycastResultCache = new List<RaycastResult>();

        protected static bool IsScreenPositionOverUI(Vector2 position)
        {
            EventSystem uiEventSystem = EventSystem.current;
            if (uiEventSystem != null)
            {
                PointerEventData uiPointerEventData = new PointerEventData(uiEventSystem);
                uiPointerEventData.position = position;

                uiEventSystem.RaycastAll(uiPointerEventData, RaycastResultCache);
                return RaycastResultCache.Count > 0;
            }
            return false;
        }

        public static bool ScreeRayIntersectionPointWithoutUI(ref Vector3 result)
        {
            Vector2 point = UnityEngine.Input.mousePosition;
            if (IsScreenPositionOverUI(point)) return false;

            var ray = RaycastCamera == null
                ? Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition)
                : RaycastCamera.ScreenPointToRay(UnityEngine.Input.mousePosition);
            Vector3 direction = ray.direction;

            if (direction.y.Equals(0))
            {
                result = Vector3.zero;
                return true;
            }
            result = ray.origin + ray.direction * (-ray.origin.y / direction.y);
            return true;
        }

        public static Vector3 ScreeRayIntersectionPoint(Vector2 point)
        {
            // var ray = RaycastCamera == null
            //     ? Camera.main.ScreenPointToRay(point)
            //     : RaycastCamera.ScreenPointToRay(point);
            var ray = Camera.main.ScreenPointToRay(point);
            Vector3 direction = ray.direction;

            if (direction.y.Equals(0))
            {
                return Vector3.zero;
            }
            return ray.origin + ray.direction * (-ray.origin.y / direction.y);
        }

        public static bool GetMouseButtonDown(int button)
        {
            if (Paused) return false;
            if (button == 0 && ClickLeftConsumed) return false;
            return UnityEngine.Input.GetMouseButtonDown(button);
        }

        // 松开鼠标不受限制
        public static bool GetMouseButtonUp(int button)
        {
            return UnityEngine.Input.GetMouseButtonUp(button);
        }

        public static bool GetKeyDown(KeyCode key)
        {
            if (Paused || !KeyboardEnable) return false;
            return UnityEngine.Input.GetKeyDown(key);
        }

        public static bool GetKey(KeyCode key)
        {
            if (Paused || !KeyboardEnable) return false;
            return UnityEngine.Input.GetKey(key);
        }

        public static float MouseScrollDelta
        {
            get
            {
                if (Paused) return 0;
                return UnityEngine.Input.mouseScrollDelta.y;
            }
        }

        public static void Pause()
        {
            Paused = true;
        }

        public static void Resume()
        {
            Paused = false;
        }

        public static bool ActionSkip
        {
            get { return UnityEngine.Input.GetMouseButtonDown(1); }
        }


        protected static bool ClickLeftConsumed = false;

        public static void ResetFrame()
        {
            ClickLeftConsumed = false;
        }

        public static void ConsumeLeftClick()
        {
            ClickLeftConsumed = false;
        }
    }
}